import 'package:baichuan/src/model.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import 'package:baichuan/src/params.dart';
import 'dart:io' show Platform;

class BCHandler {
  static const MethodChannel _channel =
      const MethodChannel('com.qiaomeng.flutter/baichuan');
  static const EventChannel _eventChannel =
      const EventChannel('com.qiaomeng.flutter/baichuan_event');
  static Function(String url)? _registerNavigateUrl;

  // 开启debug
  static void debug() {
    _channel.invokeMethod('debug');
  }

  //监听消息
  static eventChannelAddListener(Function(dynamic) listener) {
    _eventChannel.receiveBroadcastStream().listen(listener);
  }

  // 初始化
  static Future<InitResultModel> asyncInit() async {
    var map = await _channel.invokeMethod('asyncInit');
    var status = map['status'];
    if (Platform.isIOS) {
      status = map['status'] == 1;
    }
    InitResultModel res = InitResultModel(status, map['code'], map['msg']);
    _channel.setMethodCallHandler((call) async {
      if (call.method == 'registerNavigateUrl') {
        if (_registerNavigateUrl != null) {
          _registerNavigateUrl!(call.arguments);
        }
      }
    });
    return res;
  }

  // 设置三方媒体应用版本号
  static void setIsvVersion(String v) {
    _channel.invokeMethod('setIsvVersion', v);
  }

  // 打开登录
  static Future<LoginResultModel> showLogin() async {
    var map = await _channel.invokeMethod('showLogin');
    var status = map['status'];
    if (Platform.isIOS) {
      status = map['status'] == 1;
    }
    var userMap = map['userInfo'];
    UserInfoResultModel info =
        UserInfoResultModel(userMap['userId'], userMap['userNick']);
    LoginResultModel res =
        LoginResultModel(status, map['code'], map['msg'], info);
    return res;
  }

  // 登出
  static Future<LoginResultModel> logout() async {
    var map = await _channel.invokeMethod('logout');
    var userMap = map['userInfo'];
    UserInfoResultModel info =
        UserInfoResultModel(userMap['userId'], userMap['userNick']);

    LoginResultModel res =
        LoginResultModel(map['status'], map['code'], map['msg'], info);
    return res;
  }

  // 是否登录
  static Future<IsLoginResultModel> isLogin() async {
    var map = await _channel.invokeMethod('isLogin');
    IsLoginResultModel res =
        IsLoginResultModel(map['status'], map['msg'], map['isLogin']);
    return res;
  }

  // 获取淘宝用户信息
  static Future<GetUserInfoResultModel> getUserInfo() async {
    var map = await _channel.invokeMethod('getUserInfo');
    var userMap = map['userInfo'];
    UserInfoResultModel info =
        UserInfoResultModel(userMap['userId'], userMap['userNick']);
    GetUserInfoResultModel res =
        GetUserInfoResultModel(map['status'], map['code'], map['msg'], info);
    return res;
  }

  // 打开电商套件页面
  static Future<OpenResultModel> openByCode(
      {@required String? suiteCode,
      AlibcBizParams? biz,
      AlibcTaokeParams? taoke,
      AlibcShowParams? show,
      Map? trackParams}) async {
    var map = await _channel.invokeMethod('openByCode', {
      "suiteCode": suiteCode,
      "bizParams": biz != null ? biz.toMap() : {},
      "showParams": show != null ? show.toMap() : {},
      "taokeParams": taoke != null ? taoke.toMap() : {},
      "trackParams": trackParams
    });
    bool status;
    if (Platform.isIOS) {
      status = map['status'] == 1;
    } else {
      status = map['status'];
    }
    OpenResultModel m = OpenResultModel(status, map['code'], map['msg']);
    return m;
  }

  // 打开普通电商页面
  static Future<OpenResultModel> openByUrl(
      {@required String? detailUrl,
      AlibcTaokeParams? taoke,
      AlibcShowParams? show,
      Map? trackParams}) async {
    var map = await _channel.invokeMethod('openByUrl', {
      "detailUrl": detailUrl,
      "showParams": show == null ? {} : show.toMap(),
      "taokeParams": taoke == null ? {} : taoke.toMap(),
      "trackParams": trackParams ?? {}
    });
    bool status;
    if (Platform.isIOS) {
      status = map['status'] == 1;
    } else {
      status = map['status'];
    }
    OpenResultModel res = OpenResultModel(status, map['code'], map['msg']);

    return res;
  }

  // 注册 媒体外跳页面路由协议
  static void registerNavigateUrl(Function(String url) cb) {
    _channel.invokeMethod('registerNavigateUrl');
    _registerNavigateUrl = cb;
  }

  // 注册相关图片库协议
  static void registerImage() {
    _channel.invokeMethod('registerImage');
  }
}
